<?php
require 'key.php';
require 'modifyUserMod.php';

$query=$_SERVER['QUERY_STRING'];
parse_str($query,$queryResult);

if($queryResult['key']===$manageKey)
{
    $isJump=true;

    if(array_key_exists('userName',$_POST) && array_key_exists('newUser',$_POST)) //变更机器码
    {
        $userName=$_POST['userName'];
        $newUser=$_POST['newUser'];
        if(!modifyUser($userName,$newUser))
        {
            $isJump=false;
            echo "<script>alert('未找到需变更机器码的用户名');
                    history.back();</script>";
        }
    }
    else //变更视频许可
    {
        $data=parse_ini_file('./data.ini');
        $allVideo=explode('；',$_POST["video"]);
        foreach ($data as $varName => $val) //去除video
        {
            $s=split_code($varName);
            $user=$s[0];
            $video=$s[1];
            if($user===$_POST["user"])
            {
                if(!in_array($video,$allVideo)) //不在新的video表里
                    $data[$varName]='0';
            }
        }
        foreach ($allVideo as $video) //添加video
        {
            $varName=joint_code($_POST["user"],$video);
            $data[$varName]='1';
        }
        write_ini_file('./data.ini',$data); //保存修改
    }

    if($isJump)
        header("location:manage.php?key=".$manageKey);
}